/*
 * Copyright (C) 2011 Project R'yleh Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.Arkham;

import java.util.ArrayList; 

public class Inventory {
	private int             money;
    private ArrayList<Item> inventory;
    
    public Inventory(){
    	money = 0;
    	inventory = new ArrayList<Item>();
    }
    
    public int getMoney(){
    	return money;
    }
    
    public boolean setMoney(int cash){
    	money = cash;
    	return true;
    }
    
    public boolean gainMoney(int cash){
    	money += cash;
    	return true;
    }

    public int size(){
    	if (inventory == null)
    		return 0;
    	
    	return inventory.size();
    }

    public Item get(int index){
    	return inventory.get(index);
    }
    
    public boolean sellItem(int index){
    	if(index >= inventory.size())
    		return false;
    	if(isEquipped(index)){
    		//item is equiped and needs removed.
    		unEquip(inventory.get(index));
    	}
    	Item soldItem = inventory.remove(index);
    	money += soldItem.getSellPrice();
    	return true;
    }
    
    public boolean contains(Item item){

    	for (int i=0; i<inventory.size(); i++)
    		if( inventory.get(i).getName() == item.getName() )
    			return true;

    	return false;
    }
    
    public boolean purchaseItem(Item purchased){
    	if(purchased.getPurchasePrice() > money)
    		return false;
    	money -= purchased.getPurchasePrice();
    	return inventory.add(purchased);
    }
    
    public boolean useItem(int index){
    	Item used = inventory.get(index);
    	if(!used.isItemType(Constants.USABLE_CONSUMABLE) && 
    	   !used.isItemType(Constants.USABLE_NONCONSUMABLE))
    		return false;
    	if(used.isItemType(Constants.USABLE_CONSUMABLE))
    	   inventory.remove(index);
    	return true;
    }
    
    public boolean gainItem(Item gained){
    	inventory.add(gained);
    	return true;
    }
    
    public boolean equip(int index, String Who, int where){
    	//if there is none in inventory
    	if(index >= inventory.size())
    		return false;

    	//else we have one, so equip
    	Item toEquip = inventory.get(index);
    	
    	// ensure item is equippable
    	if(!toEquip.isEquipable(where))
    		return false;
    	
    	//item IS equippable, so equip
    	toEquip.setEquippedBy(Who);
    	toEquip.setEquippedAt(where);
    	inventory.set(index, toEquip);
    	return true;
    }
    
    public boolean unEquip(Item item){
    	
    	int index = inventory.indexOf(item);
    	
    	//if there is none in inventory
    	if(index == -1)
    		return false;

    	//else we have one, so unequip
    	item = inventory.get(index);
    	item.setEquippedBy(null);
    	item.setEquippedAt(-1);
    	inventory.set(index, item);
    	return true;	
    }
    
    public boolean isEquipped(int index){

        if(inventory.get(index).getEquippedAt() == -1)
        	return false;
    	
    	return true;
    }
    
    public String whoEquipped(int index){
    	return inventory.get(index).getEquippedBy();
    }
    
    public void drop(int index){
    	inventory.remove(index);
    }
}